package com.hp.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.entity.MedicalAdvice;
import com.hp.entity.Registry;
import com.hp.entity.User;
import com.hp.entity.dto.RegistryDto;
import com.hp.entity.dto.UserDto;
import com.hp.service.RegistryService;
import com.hp.service.UserService;
import com.hp.utils.Page;
import com.hp.utils.PasswordUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "RegistryServlet",value = "/registryServlet")
public class RegistryServlet extends HttpServlet {
    private ObjectMapper mapper=new ObjectMapper();
    private RegistryService registryService=new RegistryService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String method = request.getParameter("method");
        if(method!=null&&method.equals("createRegistry")){
            createRegistry(request,response);
        } else if(method!=null&&method.equals("getRegistryListByPage")){
            getRegistryListByPage(request,response);
        }else if(method!=null&&method.equals("updateRegistryStatus")){
            updateRegistryStatus(request,response);
        }


    }

    private void updateRegistryStatus(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        Map<String, Object> map = new HashMap<>();
        Registry registry=new Registry();
        try {
            registry.setStatus(Integer.parseInt(request.getParameter("status")));
            registry.setId(Integer.parseInt(request.getParameter("registryId")));
            Date currentDate=new Date();
            registry.setUpdatedTime(currentDate);
            registry.setConsultationTime(currentDate);

            if(registryService.updateRegistryStatus(registry)){
                map.put("code",1001);
                map.put("msg","更新挂号单成功");
            }else{
                map.put("code",1002);
                map.put("code","更新挂号单成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    private void getRegistryListByPage(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        String departmentId = request.getParameter("departmentId");
        String idCard = request.getParameter("idCard");
        String userId = request.getParameter("userId");
        String status = request.getParameter("status");
        map.put("departmentId",departmentId);
        map.put("idCard",idCard);
        map.put("userId",userId);
        map.put("status",status);

        Page<RegistryDto> page=registryService.getRegistryListByPage(map);
        map.put("page",page);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String json=mapper.writeValueAsString(page);
        response.getWriter().print(json);
    }


    private void createRegistry(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        Registry registry=new Registry();
        try {
            DateConverter converter = new DateConverter();
            converter.setPattern(new String("yyyy-MM-dd"));
            ConvertUtils.register(converter,Date.class);
            BeanUtils.populate(registry,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //设置其他字段值
        Date currentDate=new Date();
        registry.setCreatedTime(currentDate);
        registry.setUpdatedTime(currentDate);
        registry.setRegistryTime(currentDate);
        registry.setStatus(1);

        System.out.println(registry);

        //添加用户
        if(registryService.addRegistry(registry)){
            map.put("code",1001);
            map.put("msg","创建挂号单成功");
        }else{
            map.put("code",1002);
            map.put("msg","创建挂号单失败");
        }
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
